<?php

namespace app\admin\model\admin;

use think\Model;
use think\Session;
use think\Db;
/**
 * 超管后台用户模型
 * Class User
 * @package app\admin\model\admin
 */
class User extends Model{
    protected $name='love_admin_user';
    /**
     * 超管后台用户登录
     */
    public function login($data){
        $user=Db::name('love_admin_user')->where(['user_name' => $data['user_name']])->find();
        if(!$user){
            $this->error = '登录失败, 用户名不存在';
            return false;
        }else{
            if($user['login_type']==2){
                $this->error = '账号类型不正确';
                return false;
            }
            if($user['is_delete']==1){
                $this->error = '该账号已被禁用';
                return false;
            }
            $pass= md5($data['password']);
            if($pass==$user['password']){
                // 保存登录状态
                if($user['areaid']=='0'){
                    $name=$user['name'];

                }elseif ($user['areaid']=='all'){
                    $ar=Db::name('love_area')->where(['id'=>$user['cityid']])->value('name');
                    $name=$ar.'--'.$user['name'];
                }else{
                    $ar=Db::name('love_area')->where(['id'=>$user['areaid']])->value('name');
                    $name=$ar.'--'.$user['name'];
                }
                Session::set('yoshop_admin', [
                    'user' => [
                        'id' => $user['id'],
                        'user_name' => $user['user_name'],
                        'name'=>$name
                    ],
                    'is_login' => 1,
                ]);
                return true;
            }else{
                $this->error = '登录失败, 用户名或密码错误';
                return false;
            }
        }
    }

    /**
     * 超管用户信息
     * @param $admin_user_id
     * @return null|static
     * @throws \think\exception\DbException
     */
    public static function detail($id){
        return self::get($id);
    }

    /**
     * 更新当前管理员信息
     */
    public function renew($data){
        if ($data['password'] !== $data['password_confirm']) {
            $this->error = '确认密码不正确';
            return false;
        }
        // 更新管理员信息
        if ($this->save([
            'user_name'=> $data['user_name'],
            'password' => md5($data['password']),
            ]) === false) {
            return false;
        }
        // 更新session
        $user=Db::name('love_admin_user')->where(['id'=>$this['id']])->find();
        if($user['areaid']==0){
            $name=$user['name'];
        }elseif ($user['areaid']=='all'){
            $ar=Db::name('love_area')->where(['id'=>$user['cityid']])->value('name');
            $name=$ar.'--'.$user['name'];
        }else{
            $ar=Db::name('love_area')->where(['id'=>$user['areaid']])->value('name');
            $name=$ar.'--'.$user['name'];
        }
        Session::set('yoshop_admin.user', [
            'id' => $this['id'],
            'user_name' => $data['user_name'],
            'name'=>$name
        ]);
        return true;
    }
    /**
     * 验证用户名是否重复
     * @param $userName
     * @return bool
     */
    public static function checkExist($userName){
        return !!static::useGlobalScope(false)
            ->where('user_name', '=', $userName)
            ->where('is_delete', '=', 0)
            ->value('id');
    }

    /**
     * 获取用户列表
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList(){
        return $this->where('is_delete', '=', '0')
            ->order(['create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => \request()->request()
            ]);
    }


}